package com.android.kotlincoroutinemvvmdemo
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.kotlincoroutinemvvmdemo.adapter.CommonRecyclerViewPool
import com.android.kotlincoroutinemvvmdemo.adapter.OneFiAdapter
import com.android.kotlincoroutinemvvmdemo.base.BaseActivity
import com.android.kotlincoroutinemvvmdemo.databinding.ActivityMainBinding
import com.android.kotlincoroutinemvvmdemo.util.LogX
import com.android.kotlincoroutinemvvmdemo.util.UrlConstant
import com.android.kotlincoroutinemvvmdemo.viewmodel.OneFiViewModel
import kotlinx.coroutines.launch
import kotlin.concurrent.thread

class MainActivity : BaseActivity<ActivityMainBinding>() {

    private lateinit var mAdapter: OneFiAdapter

    private lateinit var mViewModel: OneFiViewModel

    override fun getLayoutId() = R.layout.activity_main

    private val mReceiver by lazy {
        MainBroadcastReceiver()
    }

    private val mFilter by lazy {
        IntentFilter().apply {
            addAction(Constants.ACTION_IPC)
        }
    }

    override fun init(savedInstanceState: Bundle?) {

        mViewModel = ViewModelProvider(this)[OneFiViewModel::class.java]

        mAdapter = OneFiAdapter(this@MainActivity)
        mBinding.contentRv.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter = mAdapter
            setRecycledViewPool(CommonRecyclerViewPool)
        }

        mViewModel.getOneFiDataSet()

        lifecycleScope.launch {
            mViewModel.mOneFiFlow.collect {
                mAdapter.setDataSet(it)
            }
        }

        LogX.d(Constants.TAG, "MainActivity---onCreate---processId:${android.os.Process.myPid()}")

        registerReceiver(mReceiver, mFilter)

        thread {
            //线程已经启动起来，没有被抢占资源或者锁，就会一直运行下去。
            Thread.sleep(10 * 1000)
            LogX.d(Constants.TAG, "Thread.sleep(10 * 1000)")
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LogX.d(Constants.TAG, "Build.VERSION.SDK_INT >= Build.VERSION_CODES.O")
            startForegroundService(Intent().apply {
                `package` = "com.android.kotlincoroutinemvvmdemo"
                setClassName("com.android.kotlincoroutinemvvmdemo","com.android.kotlincoroutinemvvmdemo.DemoService")
            })

//            startService(Intent().apply {
//                `package` = "com.android.kotlincoroutinemvvmdemo"
//                setClassName("com.android.kotlincoroutinemvvmdemo","com.android.kotlincoroutinemvvmdemo.DemoService")
//            })
        }
    }

    override fun onStart() {
        super.onStart()
        LogX.d(Constants.TAG, "MainActivity---onStart")
    }

    override fun onResume() {
        super.onResume()
        LogX.d(Constants.TAG, "MainActivity---onResume")

        LogX.d(TAG, UrlConstant.REQUEST_TYPE)
        LogX.d(TAG, UrlConstant.REQUEST_GAME_LIKE)
    }

    override fun onPause() {
        super.onPause()
        LogX.d(Constants.TAG, "MainActivity---onPause")
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(mReceiver)
    }

    override fun adapterUIModeNightForActivity(uiModeNight: Boolean) {
        super.adapterUIModeNightForActivity(uiModeNight)
        LogX.d(TAG, "uiModeNight:${uiModeNight}")
        if (uiModeNight) {
            mBinding.clRoot.setBackgroundColor(resources.getColor(R.color.black))
        } else {
            mBinding.clRoot.setBackgroundColor(resources.getColor(R.color.white))
        }

        mAdapter.adapterUIModeNightForRecyclerView(uiModeNight)

        adapterStatusBar(R.color.purple_200)
    }


    private inner class MainBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (intent?.action) {
                Constants.ACTION_IPC -> {
                    LogX.d(Constants.TAG,"ipc successful.")
                }
            }
        }
    }

    companion object {
        const val TAG = "MainActivity"
    }
}